package fr.coding.kata.nantes.bowling.michel;

public class Roll {
    private int value;
    private Roll previous;
    private Roll next;
    private boolean strike;

    public boolean isStrike() {
        return strike;
    }

    public boolean isSpare() {
        return spare;
    }

    private boolean spare;

    public static Roll newInstance(char value) {
        Roll roll = new Roll();
        if ('X' == value) {
            roll.strike = true;
            roll.value = 10;
        }
        roll.spare = '/' == value;
        if (!roll.strike && !roll.spare && '-' != value)
            roll.value = Integer.parseInt(String.valueOf(value));
        return roll;
    }

    public int calculateScore() {
        int score = value;
        if (strike)
            score += nextTwoValue();
        else if (spare)
            score += nextValue();
        return score;
    }

    public int getValue() {
        return value;
    }

    public Roll getNext() {
        return next;
    }

    public void setNext(Roll next) {
        this.next = next;
    }

    public int nextValue() {
        if (null != next)
            return next.getValue();
        return 0;
    }

    public int nextTwoValue() {
        if (null == next)
            return 0;
        if (null == next.getNext())
            return nextValue();
        else
            return nextValue() + next.nextValue();
    }

    public Roll getPrevious() {
        return previous;
    }

    public void setPrevious(Roll previous) {
        this.previous = previous;
        if (spare)
            value = 10 - previous.getValue();
    }

    @Override
    public String toString() {
        return "Roll [value=" + value + ", previous=" + previous + ", next="
                + next + ", strike=" + strike + ", spare=" + spare + "]";
    }

}
